package router

import (
	"github.com/gin-gonic/gin"
	api_c "ginweb/app/api/controller"
	console_c "ginweb/app/console/controller"
	common_c "ginweb/app/common/controller"
	index_c "ginweb/app/index/controller"
	"ginweb/system"
	"github.com/gin-gonic/contrib/sessions"
	"net/http"
	"ginweb/app/console/middleware"
)

func Init() {
	router := gin.Default()
	router.LoadHTMLGlob("templates/***/**/*")
	store, _ := sessions.NewRedisStore(10, "tcp", system.GetConfiguration().RedisAddr, system.GetConfiguration().RedisPwd, []byte("secret"))
	router.Use(sessions.Sessions("session", store))



	api := router.Group("/api")
	console := router.Group("/console")
	common := router.Group("/common")
	index := router.Group("/index")

	//api路由
	api.GET("/index", api_c.Index)

	//console路由

	console.GET("/login", console_c.Login)  //登录显示页
	console.GET("/reg", console_c.Reg)      //注册显示页
	console.GET("/signOut", console_c.SignOut)      //退出登录
	console.GET("/captcha", console_c.Captcha)   // 验证码
	console.POST("/auth", console_c.Auth)   // 登录认证
	console.POST("/register", console_c.Register)  //注册
	console.GET("/sessionSet", console_c.SessionSet)  //设置session
	console.GET("/sessionGet", console_c.SessionGet)  //获取session
	console.Use(middleware.AuthConsole())
	{
		console.GET("/index", console_c.Index)
		console.GET("/about", console_c.About)
	}

	//common 路由
	common.GET("/index", common_c.Index)
	common.GET("/testMongo", common_c.TestMongodb)
	common.GET("/testRedis", common_c.TestRedis)

	//index 路由
	index.GET("/index", index_c.Index)

	router.StaticFS("/static", http.Dir("public/static"))

	router.StaticFile("favicon.ico", system.GetConfiguration().IcoPath)
	router.Run(system.GetConfiguration().Port)
}